package com.fjp.heartguard.ui.login

import android.content.Intent
import android.os.Bundle
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.widget.CompoundButton
import com.fjp.heartguard.app.App
import com.fjp.heartguard.app.Constants
import com.fjp.heartguard.app.base.BaseActivity
import com.fjp.heartguard.app.ext.showToast
import com.fjp.heartguard.app.util.CacheUtil
import com.fjp.heartguard.databinding.ActivityLoginBinding
import com.fjp.heartguard.viewmodel.LoginViewModel
import com.lxj.xpopup.XPopup
import me.hgj.jetpackmvvm.ext.parseState
import me.hgj.jetpackmvvm.ext.util.isPhone

class LoginActivity : BaseActivity<LoginViewModel, ActivityLoginBinding>() {
    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewModel = mViewModel
        mDatabind.click = ClickProxy()
        val password = CacheUtil.getPassword()
        if (password.isNotEmpty()) {
            mDatabind.savePasswordCb.isChecked = true
            mViewModel.password.value = password
            mViewModel.mobile.value = CacheUtil.getMobile()
        }
    }

    inner class ClickProxy {
        fun clickLogin() {
//            showErrorPop()
            if (!mViewModel.mobile.value.isPhone()) {
                showToast("请输入正确的手机号")
                return
            }
            if (mDatabind.savePasswordCb.isChecked) {
                CacheUtil.setPassword(mViewModel.password.value!!)
                CacheUtil.setMobile(mViewModel.mobile.value!!)
            } else {
                CacheUtil.setPassword("")
            }
            mViewModel.loginReq()

        }

        var savePasswordOnCheckedChangeListener = CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
            CacheUtil.setPassword(mViewModel.password.value!!)
        }
        var showPasswordOnCheckedChangeListener = CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                mDatabind.passwordEt.transformationMethod = HideReturnsTransformationMethod.getInstance()
                mDatabind.passwordEt.setSelection(mDatabind.passwordEt.text.length)
            } else {
                mDatabind.passwordEt.transformationMethod = PasswordTransformationMethod.getInstance()
                mDatabind.passwordEt.setSelection(mDatabind.passwordEt.text.length)
            }
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.mobile.observe(this) {
            mViewModel.enableLogin.value = !(mViewModel.password.value?.length == 0 || mViewModel.mobile.value?.length == 0)
        }
        mViewModel.password.observe(this) {
            mViewModel.enableLogin.value = !(mViewModel.password.value?.length == 0 || mViewModel.mobile.value?.length == 0)
        }
        mViewModel.loginResult.observe(this) {
            parseState(it, {
                val intent = Intent(this@LoginActivity, VerificationCodeActivity::class.java)
                intent.putExtra(Constants.INTENT_PHONE, mViewModel.mobile.value)
                intent.putExtra(Constants.INTENT_USER_INFO, it)
                startActivity(intent)
                finish()
            }, {
                it.printStackTrace()
                showErrorPop()
            })
        }
    }

    fun showErrorPop() {
        XPopup.Builder(this).asConfirm("密码错误", "手机号或密码是否正确", "取消", "确定", {}, {}, true).show()
    }
}